<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item prop="plotId" label="小区名称：">
        <el-select v-model="queryParams.plotId" placeholder="请选择小区名称" clearable style="width: 170px"
          @change="xiaoquChange">
          <el-option v-for="dict in xiaoquData" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item prop="buildId" label="所属楼栋：">
        <el-select v-model="queryParams.buildId" placeholder="请选择楼栋" clearable style="width: 170px" @change="loudonChange">
          <el-option v-for="dict in loudongData" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="单元名称：" prop="unitName">
        <el-input v-model="queryParams.unitName" placeholder="请输入单元名称" clearable />
      </el-form-item> -->
      <el-form-item label="选择单元：" prop="unitId">
        <el-select v-model="queryParams.unitId" placeholder="请选择单元" clearable style="width: 170px">
          <el-option v-for="dict in danyuanData" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['buildUnit:buildUnit:add']">新增单元</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" @click="pilinagHandleCilcik"
          v-hasPermi="['buildUnit:buildUnit:remove']">批量删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" @click="pilinagBindModel"
          v-hasPermi="['buildUnit:buildUnit:edit']">绑定模型</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['buildUnit:buildUnit:export']">导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="buildUnitList" @selection-change="handleSelectionChange">
      <el-table-column v-if="selectFlag" show-overflow-tooltip type="selection" align="center" />
      <!-- <el-table-column show-overflow-tooltip label="单元id" align="center" prop="id" /> -->
      <!-- <el-table-column show-overflow-tooltip label="小区id" align="center" prop="lotId" /> -->
      <el-table-column width="80px" show-overflow-tooltip label="序号" align="center" type="index"></el-table-column>
      <el-table-column show-overflow-tooltip label="小区" align="center" prop="lotName" />
      <!-- <el-table-column show-overflow-tooltip label="楼栋id" align="center" prop="buildId" /> -->
      <el-table-column show-overflow-tooltip label="楼栋" align="center" prop="buildName" />
      <el-table-column show-overflow-tooltip label="单元名称" align="center" prop="unitName" />
      <el-table-column show-overflow-tooltip label="楼层数" align="center" prop="floorNumber" />
      <el-table-column show-overflow-tooltip label="供热户数" align="center" prop="heatNumber" />
      <el-table-column show-overflow-tooltip label="总户数" align="center" prop="totalHousehold" />
      <el-table-column show-overflow-tooltip label="供热面积" align="center" prop="heatArea" />
      <el-table-column show-overflow-tooltip label="热计量方式" align="center" prop="heatMeter" >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_heat_meter"
            :value="scope.row.heatMeter"
          />
        </template>
      </el-table-column>
      <el-table-column width="120px" label="当前模型" align="center" prop="customModels">
        <!-- <template slot-scope="scope">
          <div v-for="item, index in scope.row.customModels" :key="index">
            {{ item.modelName }}
          </div>
        </template> -->

        <template slot-scope="scope">
          <el-tooltip placement="top">
            <div slot="content">
              <div style="text-align: center; margin-bottom: 5px" :key="index" v-for="(item, index) in scope.row.customModels
                ? scope.row.customModels
                : []">
                {{ item.modelName }}
              </div>
            </div>
            <div style="
                cursor: pointer;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
              ">
              {{
                scope.row.customModels && scope.row.customModels.length > 0
                ? scope.row.customModels[0].modelName
                : ""
              }}
            </div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="创建人" align="center" prop="createName" />
      <el-table-column show-overflow-tooltip label="创建时间" align="center" prop="createTime" />
      <el-table-column show-overflow-tooltip label="最近更新人" align="center" prop="updateName" />
      <el-table-column show-overflow-tooltip label="最近更新时间" align="center" prop="updateTime" />
      <!-- <el-table-column show-overflow-tooltip label="备注" align="center" prop="remark" /> -->
      <el-table-column width="180" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="equipmentInfo(scope.row)"
            v-hasPermi="['buildUnit:buildUnit:edit']">设备详情</el-button>
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)"
            v-hasPermi="['buildUnit:buildUnit:edit']">修改</el-button>
          <!-- <el-button size="mini" type="text" icon="el-icon-edit"
            @click="$router.push({ path: '/editView/preview', query: scope.row })"
            v-hasPermi="['buildUnit:buildUnit:editView']">编辑视图</el-button> -->
          <el-button size="mini" type="text" @click="handleDelete(scope.row)"
            v-hasPermi="['buildUnit:buildUnit:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改楼栋单元对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-position="left" label-width="80px" style="
          display: flex;
          flex-wrap: wrap;
          justify-content: space-between;
          padding: 0 30px;
        ">
        <el-form-item prop="lotId" label="小区名称：">
          <el-select v-model="form.lotId" placeholder="请选择小区" clearable @change="xiaoquChangeAdd()">
            <el-option v-for="dict in xiaoquDataAdd" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item prop="buildId" label="所属楼栋：">
          <el-select v-model="form.buildId" placeholder="请选择楼栋" clearable>
            <el-option v-for="dict in loudongDataAdd" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="一级模型" prop="modelIds1">
          <el-select v-model="form.modelIds1" placeholder="请选择" @change="$forceUpdate()">
            <el-option
              v-for="(item, index) in CustomModelSelect1"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级模型">
          <el-select v-model="form.modelIds2" placeholder="请选择" multiple @change="$forceUpdate()">
            <el-option
              v-for="(item, index) in CustomModelSelect2"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="一级模型" :rules="[{ required: true, message: '请选择模型', trigger: 'blur' }]">
          <el-select v-model="formModelSelect1" placeholder="请选择">
            <el-option v-for="(item, index) in CustomModelSelect1" :key="index" :label="item.label"
              :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级模型">
          <el-select v-model="formModelSelect2" placeholder="请选择" multiple>
            <el-option v-for="(item, index) in CustomModelSelect2" :key="index" :label="item.label"
              :value="item.value"></el-option>
          </el-select>
        </el-form-item> -->
        <!--<el-form-item label="小区id" prop="lotId">-->
        <!--<el-input v-model="form.lotId" placeholder="请输入小区id" />-->
        <!--</el-form-item>-->
        <!--<el-form-item label="楼栋id" prop="buildId">-->
        <!--<el-input v-model="form.buildId" placeholder="请输入楼栋id" />-->
        <!--</el-form-item>-->
        <el-form-item label="单元名称" prop="unitName">
          <el-input v-model="form.unitName" placeholder="请输入单元名称" />
        </el-form-item>
        <el-form-item label="楼层数" prop="floorNumber">
          <el-input v-model="form.floorNumber" placeholder="请输入楼层数" />
        </el-form-item>
        <!-- <el-form-item label="供热户数" prop="heatNumber">
          <el-input v-model="form.heatNumber" placeholder="请输入供热户数" />
        </el-form-item> -->
        <!-- <el-form-item label="总户数" prop="totalHousehold">
          <el-input v-model="form.totalHousehold" placeholder="请输入总户数" />
        </el-form-item> -->
        <!-- <el-form-item label="供热面积" prop="heatArea">
          <el-input v-model="form.heatArea" placeholder="请输入供热面积" />
        </el-form-item> -->
        <el-form-item label="热计量方式" prop="heatMeter">
          <!-- <el-input v-model="form.heatMeter" placeholder="请输入热计量方式" /> -->
          <el-select v-model="form.heatMeter" placeholder="请选择热计量方式">
            <el-option v-for="dict in dict.type.sys_heat_meter" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <div style="width: 100%">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 
    <el-dialog title="绑定模型" :visible.sync="dialogMx" width="400px">
      <el-form ref="mxForm" :rules="moxRules" :model="moxForm" style="background: unset;padding: 0;margin: 0;">
        <el-form-item label="绑定模型:" prop="value" style="margin-right: 60px;">
          <el-select v-model="moxForm.value" placeholder="请选择">
            <el-option v-for="(item, index) in mxData" :key="index" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogMx = false">取 消</el-button>
        <el-button type="primary" @click="selectMoxOk">确 定</el-button>
      </div>
    </el-dialog> -->

    <el-dialog title="绑定模型" :visible.sync="dialogMx" width="500px" class="bindModel">
      <el-form ref="mxForm" :rules="moxRules" :model="moxForm" style="background: unset">
        <el-form-item label="一级模型:" prop="formModelSelect1">
          <el-select v-model="moxForm.formModelSelect1" placeholder="请选择">
            <el-option v-for="(item, index) in CustomModelSelect1" :key="index" :label="item.label"
              :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级模型:">
          <el-select v-model="moxForm.formModelSelect2" placeholder="请选择" multiple>
            <el-option v-for="(item, index) in CustomModelSelect2" :key="index" :label="item.label"
              :value="item.value"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogMx = false">取 消</el-button>
        <el-button type="primary" @click="selectMoxOk">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="设备列表" :visible.sync="dialogSheBei">
      <div><el-button type="primary" @click="addEqu">添加设备</el-button></div>
      <el-table class="scroll_box" :data="shebeiList" style="height: 300px; overflow-y: scroll">
        <el-table-column show-overflow-tooltip align="center" type="index" label="序号"></el-table-column>
        <el-table-column show-overflow-tooltip align="center" label="设备种类" prop="equipmentTypeName"></el-table-column>
        <el-table-column show-overflow-tooltip align="center" label="设备名称" prop="equipmentName"></el-table-column>
        <el-table-column show-overflow-tooltip align="center" label="设备编号" prop="equipmentCode"></el-table-column>
        <el-table-column show-overflow-tooltip align="center" label="所属单元">{{ addSheBeiForm.unitName }}</el-table-column>
        <el-table-column show-overflow-tooltip align="center" label="详细位置" prop="address"></el-table-column>
        <el-table-column show-overflow-tooltip align="center" label="所属设备分组" prop="equipmentGroupName"></el-table-column>
        <el-table-column show-overflow-tooltip label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="shebeiEdit(scope.row)"
              v-hasPermi="['build:build:remove']">编辑</el-button>
            <el-button size="mini" type="text" @click="shebeiDel(scope.row)"
              v-hasPermi="['build:build:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="shebeitotal > 0" :total="shebeitotal" :page.sync="shebeiqueryParams.pageNum"
        :limit.sync="shebeiqueryParams.pageSize" @pagination="sheibei" />
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogSheBei = false">取 消</el-button>
        <el-button type="primary" @click="dialogSheBei = false">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="equTitle" :visible.sync="addShebeiDiaLOG" width="700px">
      <el-form ref="addSheBei" :model="addSheBeiForm" :rules="addSheBeiFormRule" style="
          height: unset;
          background: unset;
          margin: 0;
          padding: 0;
          display: flex;
          justify-content: space-between;
          flex-wrap: wrap;
        ">
        <el-form-item label="所属单元" prop="unitName" style="margin-bottom: 30px">
          <el-input v-model="addSheBeiForm.unitName" disabled="" placeholder=""></el-input>
        </el-form-item>
        <el-form-item label="dtuId" prop="dtuId" style="margin-bottom: 30px">
          <el-select v-model="addSheBeiForm.dtuId" placeholder="请选择dtuId" clearable>
            <el-option v-for="dict in dtuIdSelectList" :key="dict.id" :label="dict.name" :value="dict.id" />
          </el-select>
        </el-form-item>

        <el-form-item label="设备种类" prop="equipmentType" style="margin-bottom: 30px">
          <el-select v-model="addSheBeiForm.equipmentType" placeholder="请选择设备种类" clearable>
            <el-option v-for="(item, index) in shebeiDataAdd" :key="index" :label="item.dictLabel"
              :value="item.dictValue"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="协议类型" prop="protocolType" style="margin-bottom: 30px">
          <el-select :disabled="!addSheBeiForm.equipmentType" v-model="addSheBeiForm.protocolType" placeholder="请选择协议类型"
            clearable>
            <el-option v-for="dict in conputedAgreementSelectList" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue" />
          </el-select>
        </el-form-item>

        <el-form-item label="设备名称" prop="equipmentName" style="margin-bottom: 30px">
          <el-input v-model="addSheBeiForm.equipmentName" placeholder="设备名称"></el-input>
        </el-form-item>

        <el-form-item label="设备编号" prop="equipmentCode" style="margin-bottom: 30px">
          <el-input v-model="addSheBeiForm.equipmentCode" placeholder="设备编号"></el-input>
        </el-form-item>

        <el-form-item label="设备地址" prop="address" style="margin-bottom: 30px">
          <el-input v-model="addSheBeiForm.address" placeholder="设备地址"></el-input>
        </el-form-item>

        <el-form-item label="设备分组" prop="equipmentGroupId" style="margin-bottom: 30px">
          <!--<el-input v-model="addSheBeiForm.equipmentGroupId" placeholder=""></el-input>-->
          <el-select v-model="addSheBeiForm.equipmentGroupId" placeholder="请选择设备分组" clearable>
            <el-option v-for="(item, index) in shbeiFenZuGroups" :key="index" :label="item.groupName"
              :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="主动抄表" prop="autoRead" style="position: unset">
          <el-select v-model="addSheBeiForm.autoRead" placeholder="是否主动抄表">
            <el-option v-for="(item, index) in chaobiaoData" :key="index" :label="item.label"
              :value="item.value"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addShebeiDiaLOG = false">取 消</el-button>
        <el-button type="primary" @click="isOk">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBuildUnit,
  getBuildUnit,
  delBuildUnit,
  addBuildUnit,
  updateBuildUnit,
} from "@/api/buildUnit/buildUnit";
import { unitBuildList, equipmentListInfo } from "@/api/mock";
import {
  listCPlotSelect,
  listBuildSelect,
  listBuildUnitSelect,
  listCustomModelSelect,
  listEquipmentGroupSelect,
} from "@/api/system/utils";
import {
  listEquipment,
  getEquipment,
  addEquipment,
  updateEquipment,
  delEquipment,
} from "@/api/equipment/equipment";
import { batchBindModel } from "@/api/customModel/customModel";
import { dtuSelectList } from "@/api/equipmentMessage/equipmentMessage";
import { getDicts } from "@/api/system/dict/data";

export default {
  name: "BuildUnit",
  dicts: ["sys_heat_meter"],
  data() {
    return {
      //小区一级模型下拉菜单
      CustomModelSelect1: [],
      //小区二级模型下拉菜单
      CustomModelSelect2: [],
      //小区一级模型选择
      formModelSelect1: [],
      //小区二级模型选择
      formModelSelect2: null,
      // 设备
      dialogSheBei: false,
      equTitle: "",
      buildId: "",
      lotId: "",
      unitId: "",
      unitName: "",
      shebeiList: [],
      dtuIdSelectList: [],
      agreementSelectList: [
        {
          dictLabel: "单元阀门协议",
          dictValue: "2",
        },
        {
          dictLabel: "单元热量表协议",
          dictValue: "5",
        },
      ],
      shebeiDataAdd: [
        {
          dictLabel: "阀门",
          dictValue: "1",
        },
        {
          dictLabel: "热量表",
          dictValue: "2",
        },
      ],
      shbeiFenZuGroups: [],
      shebeiqueryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      shebeitotal: 0,
      loudongData: [],
      shebeiFenzu: [
        { label: "楼栋设备组", value: 0 },
        { label: "单元设备组", value: 1 },
        { label: "住户设备组", value: 2 },
      ],

      chaobiaoData: [
        { label: "是", value: "0" },
        { label: "否", value: "1" },
      ],
      // 添加设备
      addShebeiDiaLOG: false,
      addSheBeiForm: {
        id: null,
        dtuId: "",
        unitName: "",
        equipmentType: "",
        equipmentName: "",
        equipmentCode: "",
        protocolType: "",
        address: "",
        equipmentGroupId: "",
        autoRead: "",
        equipmentLevel: 2,
      },
      addSheBeiFormRule: {
        dtuId: [{ required: true, message: "dtuId不能为空", trigger: "blur" }],
        protocolType: [
          { required: true, message: "请选择协议类型", trigger: "blur" },
        ],
        unitName: [
          { required: true, message: "请选择所属单元", trigger: "blur" },
        ],
        equipmentType: [
          { required: true, message: "请选择设备种类", trigger: "blur" },
        ],
        equipmentName: [
          { required: true, message: "请输入设备名称", trigger: "blur" },
        ],
        equipmentCode: [
          { required: true, message: "请输入设备编号", trigger: "blur" },
        ],
        address: [
          { required: true, message: "请输入设备地址", trigger: "blur" },
          {
            required: true,
            min: 14,
            max: 14,
            message: "设备地址为14位数字",
            trigger: "blur",
          },
        ],
        equipmentGroupId: [
          { required: true, message: "请选择设备分组", trigger: "blur" },
        ],
        autoRead: [
          { required: true, message: "请选择设备类型", trigger: "blur" },
        ],
      },
      // 模型
      dialogMx: false,
      moxForm: {
        formModelSelect1: "",
        formModelSelect2: [],
      },
      moxRules: {
        formModelSelect1: [
          { required: true, message: "请选择一级模型", trigger: "change" },
        ],
      },
      mxData: [
        { label: "模型D", value: 0 },
        { label: "模型F", value: 1 },
        { label: "模型H", value: 2 },
        { label: "模型V", value: 3 },
        { label: "模型T", value: 4 },
        { label: "模型Q", value: 5 },
      ],
      xiaoquData: [],
      xiaoquDataAdd: [],
      loudongDataAdd: [],
      danyuanData: [],
      selectFlag: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 楼栋单元表格数据
      buildUnitList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        unitName: null,
        unitId:null,
        areaId: null,
        biudName: null,
        buildId: null,
      },
      // 表单参数
      form: {
        modelIds1:"",
        modelIds2:[]
      },
      // 表单校验
      rules: {
        lotId: [{ required: true, message: "小区id不能为空", trigger: "blur" }],
        buildId: [
          { required: true, message: "楼栋id不能为空", trigger: "blur" },
        ],
        unitName: [
          { required: true, message: "单元名称不能为空", trigger: "blur" },
        ],
        floorNumber: [
          { required: true, message: "楼层数不能为空", trigger: "blur" },
        ],
        heatNumber: [
          { required: true, message: "供热户数不能为空", trigger: "blur" },
        ],
        totalHousehold: [
          { required: true, message: "总户数不能为空", trigger: "blur" },
        ],
        heatArea: [
          { required: true, message: "供热面积不能为空", trigger: "blur" },
        ],
        heatMeter: [
          { required: true, message: "热计量方式不能为空", trigger: "blur" },
        ],
        createBy: [
          { required: true, message: "创建者不能为空", trigger: "blur" },
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" },
        ],
        modelIds1: [
          { required: true, message: "请选择一级模型", trigger: "blur" },
        ],
      },
    };
  },

  computed: {
    conputedAgreementSelectList() {
      if (this.addSheBeiForm.equipmentType) {
        let obj = this.shebeiDataAdd.find(
          (item) => item.dictValue == this.addSheBeiForm.equipmentType
        );
        return this.agreementSelectList.filter((item) => {
          return item.dictLabel.search(obj.dictLabel) != -1;
        });
      } else {
        return this.agreementSelectList;
      }
    },
  },
  created() {
    //单元一级模型下拉菜单
    listCustomModelSelect({
      modelType: "2",
      modelLevel: "1",
    }).then((res) => {
      // console.log("小区一级模型下拉菜单",res.rows);
      this.CustomModelSelect1 = res.rows;
    });
    //单元二级模型下拉菜单
    listCustomModelSelect({
      modelType: "2",
      modelLevel: "2",
    }).then((res) => {
      // console.log("小区二级模型下拉菜单",res.rows);
      this.CustomModelSelect2 = res.rows;
    });
    // this.getList();
    // this.sheibei()
    listCPlotSelect({}).then((res) => {
      this.xiaoquData = res.rows;
      this.xiaoquDataAdd = res.rows;
      this.queryParams.plotId = res.rows[0].value
    }).then(res=>{
      listBuildSelect({ plotId:this.queryParams.plotId}).then((res) => {
          this.loudongData = res.rows;
        });
      this.getList();
    });
  },
  methods: {
    //dtuid选择列表
    getdtuSelectList() {
      if (this.dtuIdSelectList.length <= 0) {
        dtuSelectList({ pageSize: 999 }).then((res) => {
          res.rows = res.rows.map((item) => {
            item.id = item.id + "";
            return item;
          });
          this.dtuIdSelectList = res.rows;
        });
      }
    },
    getshebeiData() {
      if (this.shebeiDataAdd.length <= 0) {
        getDicts("equipment_type").then((res) => {
          this.shebeiDataAdd = res.data;
        });
      }
      if (this.agreementSelectList.length <= 0) {
        getDicts("protocol_type").then((res) => {
          this.agreementSelectList = res.data;
        });
      }
    },
    //切换小区
    xiaoquChange() {
      listBuildSelect({ plotId: this.queryParams.plotId }).then((res) => {
        this.queryParams.buildId = null;
        this.queryParams.unitId = null;
        this.loudongData = res.rows;
        this.danyuanData = [];
      });
    },
    xiaoquChangeAdd(buildIdNull) {
      if (buildIdNull == null) {
        buildIdNull = true;
      }
      listBuildSelect({ plotId: this.form.lotId }).then((res) => {
        if (buildIdNull) {
          this.form.buildId = null;
        }
        this.loudongDataAdd = res.rows;
      });
    },
    //切换楼栋
    loudonChange() {
      listBuildUnitSelect({ buildId: this.queryParams.buildId }).then((res) => {
        this.queryParams.unitId = null;
        this.danyuanData = res.rows;
      });
    },

    /** 设备详情操作 */
    equipmentInfo(row) {
      var id = null;
      if (row != null) {
        id = row.id;
        this.buildId = row.buildId;
        this.unitId = row.id;
        this.unitName = row.unitName;
        this.lotId = row.lotId;
        this.setaddSheBeiForm();
      } else {
        id = this.addSheBeiForm.unitId;
      }
      this.dialogSheBei = true;

      this.loading = true;
      listEquipment({ unitId: id }).then((response) => {
        this.shebeiList = response.rows;
        this.shebeitotal = response.total;
        this.loading = false;
      });
      listEquipmentGroupSelect({ plotId: this.addSheBeiForm.lotId }).then(
        (res) => {
          this.shbeiFenZuGroups = res.rows;
        }
      );
    },
    setaddSheBeiForm() {
      this.addSheBeiForm.buildId = this.buildId;
      this.addSheBeiForm.unitId = this.unitId;
      this.addSheBeiForm.unitName = this.unitName;
      this.addSheBeiForm.lotId = this.lotId;
    },
    // 设备修改
    shebeiEdit(row) {
      this.loading = true;
      this.getdtuSelectList();
      // this.getshebeiData()
      getEquipment(row.id).then((response) => {
        this.addSheBeiForm = response.data;
        this.addSheBeiForm.equipmentLevel = 2;
        this.setaddSheBeiForm();
        this.addShebeiDiaLOG = true;
        this.equTitle = "修改设备";
        this.loading = false;
      });
    },
    // 设备新增
    addEqu() {
      this.getdtuSelectList();
      // this.getshebeiData()
      this.equTitle = "新增设备";
      this.addShebeiDiaLOG = true;
      this.addSheBeiForm.equipmentLevel = 2;
      this.addSheBeiForm.id = null;
      this.$nextTick(() => {
        this.resetForm("addSheBei");
      });
    },
    // 设备删除
    shebeiDel(row) {
      this.$modal
        .confirm("是否确认删除设备？")
        .then(function () {
          delEquipment(row.id);
        })
        .then(() => {
          this.equipmentInfo();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    // 设备列表
    async sheibei() {
      const res = await equipmentListInfo(this.shebeiqueryParams);
      this.shebeiList = res.rows;
      this.shebeitotal = res.total;
      console.log(res);
    },
    isOk() {
      this.$refs["addSheBei"].validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.addSheBeiForm.id != null) {
            updateEquipment(this.addSheBeiForm).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.addShebeiDiaLOG = false;
              this.equipmentInfo();
              this.loading = false;
            });
          } else {
            addEquipment(this.addSheBeiForm).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.addShebeiDiaLOG = false;
              this.equipmentInfo();
              this.loading = false;
            });
          }
        }
      });
    },

    /** 查询楼栋单元列表 */
    getList() {
      this.loading = true;
      listBuildUnit(this.queryParams).then((response) => {
        // unitBuildList(this.queryParams).then(response => {
        this.buildUnitList = response.rows;
        this.total = response.total;
        this.loading = false;
      }, err => {
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        lotId: null,
        buildId: null,
        unitName: null,
        floorNumber: null,
        heatNumber: null,
        totalHousehold: null,
        heatArea: null,
        heatMeter: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        modelIds: null,
      };
      this.formModelSelect1 = [];
      this.formModelSelect2 = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.loudongData = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      // console.log(selection);
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加楼栋单元";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getBuildUnit(id).then((response) => {
        this.form = response.data;
        this.form.buildId == null
          ? ""
          : (this.form.buildId = this.form.buildId.toString());
        this.form.lotId == null
          ? ""
          : (this.form.lotId = this.form.lotId.toString());
        console.log(this.form);
        this.temp(response.data.modelIds, this.CustomModelSelect1);
        this.xiaoquChangeAdd(false);
        this.open = true;
        this.title = "修改楼栋单元";
      });
    },
    //一级二级模型回显匹配函数
    temp(modelIds, CustomModelSelect1) {
      let modelIdsList = modelIds?.split(",").filter((item) => item !== "");
      console.log("模型数组", modelIdsList);
      console.log("下拉一级模型", CustomModelSelect1);
      if (modelIdsList?.length === 1) {
        this.form.modelIds1 = modelIdsList[0];
        return;
      }
      let modelSelect1 = modelIdsList?.filter((item) => {
        return CustomModelSelect1.some((item1) => {
          console.log("item1", item1);
          return item1.value == item;
        });
      });
      console.log("一级模型", modelSelect1);
      let modelSelect2 = modelIdsList?.filter(
        (item) => item != modelSelect1.toString()
      );
      console.log("二级模型", modelSelect2);
      if(modelSelect1){
        this.form.modelIds1 = modelSelect1[0];
      }
      this.form.modelIds2 = modelSelect2;
    },
    /** 提交按钮 */
    submitForm() {
      console.log(this.form.modelIds1, this.form.modelIds2);
      //一级模型和二级模型拼接
      let formModelSelect1 = "," + this.form.modelIds1 + ",";
      let formModel = formModelSelect1;
      if (this.form.modelIds2 && this.form.modelIds2.length) {
        let formModelSelect2 = this.form.modelIds2.toString() + ",";
        formModel = formModelSelect1 + formModelSelect2;
      }
      this.form.modelIds = formModel;
      console.log(formModel);
      console.log(this.form);
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateBuildUnit(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBuildUnit(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 批量删除
    pilinagHandleCilcik() {
      const ids = this.ids;
      if (this.ids.length === 0) {
        this.selectFlag = !this.selectFlag;
      } else {
        this.$modal
          .confirm("是否删除所选数据？")
          .then(function () {
            return delBuildUnit(ids);
          })
          .then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          })
          .catch(() => { });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除楼栋单元？")
        .then(function () {
          return delBuildUnit(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    //批量绑定模型
    pilinagBindModel() {
      // this.reset()
      if (this.ids.length === 0) {
        this.selectFlag = !this.selectFlag;
      } else {
        const ids = this.ids;
        console.log(ids);
        this.reset();
        this.dialogMx = true;
      }
    },
    // 提交绑定模型
    selectMoxOk() {
      console.log(this.moxForm.formModelSelect1, this.moxForm.formModelSelect2);
      //一级模型和二级模型拼接
      let formModelSelect1 = "," + this.moxForm.formModelSelect1 + ",";
      let formModel = formModelSelect1;
      if (this.moxForm.formModelSelect2.length) {
        let formModelSelect2 = this.moxForm.formModelSelect2.toString() + ",";
        formModel = formModelSelect1 + formModelSelect2;
      }
      console.log("模型字符串", formModel);
      console.log("选择的ids", this.ids);
      this.$refs["mxForm"].validate((valid) => {
        if (valid) {
          batchBindModel({
            type: "2",
            ids: this.ids,
            modelIds: formModel,
          }).then((res) => {
            this.$modal.msgSuccess("绑定成功");
            this.dialogMx = false;
            this.getList();
          });
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "buildUnit/exportBuildUnit",
        {
          ...this.queryParams,
        },
        `buildUnit_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
.scroll_box::-webkit-scrollbar {
  width: 0px;
}

.bindModel {
  ::v-deep .el-form {
    height: 100% !important;
    background: unset;
    display: block !important;

    .el-form-item {
      margin-left: 40px;
      margin-bottom: 30px !important;
      position: static !important;
    }
  }
}

::v-deep .el-tooltip {
  max-width: 100% !important;
}
</style>
